# coding=utf-8
from django.db.models import Sum

from goods_info.models import GoodsInventoryModel


def get_inventory(goods_id,warechouse_id=0):
    '''
    获取某一个货品的当前库存，如果warehouse_id=0，查询所有仓库的库存
    '''
    sum_inventory = 0

    if warechouse_id == 0: # 所有仓库的库存总和 . select sum(price) as cur_sum form tabel
        result = GoodsInventoryModel.objects.filter(goods_id=goods_id).aggregate(cur_sum=Sum('cur_inventory'))
    else: # 查询商品ID和仓库对应的库存
        result = GoodsInventoryModel.objects.filter(goods_id=goods_id,warehouse_id=warechouse_id).aggregate(cur_sum=Sum('cur_inventory'))

    if result['cur_sum'] and result['cur_sum'] != 'None':
        sum_inventory = result['cur_sum']

    return sum_inventory






